<?php
/**
 * Tên file mình có thể tự chọn nhưng nên đặc theo têm controller sẽ dễ quản lý
 */

/**
 * Tên class: Example_Models_Dao_Pdo_Mysql_Example(Tenmodule_Models_Dao_Pdo_Mysql_Tenfile)
 */

class Enterprise_Models_Dao_Pdo_Mysql_Enterprise extends Tomato_Model_Dao
    implements Enterprise_Models_Interface_Enterprise
{
    public function convert($entity)
    {
        return new Enterprise_Models_Enterprise($entity);
    }

    public function add($enterprise){
        $this->_conn->insert($this->_prefix . 'enterprise',
                            array(
                                'frontend_active'        => $enterprise->frontend_active,
                                'backend_active'         => $enterprise->backend_active,
                                'company_name_vn'        => $enterprise->company_name_vn,
                                'company_name_en'        => $enterprise->company_name_en,
                                'contact_email'          => $enterprise->contact_email,
                                'contact_tel'            => $enterprise->contact_tel,
                                'contact_fax'            => $enterprise->contact_fax,
                                'contact_address'        => $enterprise->contact_address,
                                'bank_name_vnd'          => $enterprise->bank_name_vnd,
                                'bank_name_usd'          => $enterprise->bank_name_usd,
                                'bank_account_vnd'       => $enterprise->bank_account_vnd,
                                'bank_account_usd'       => $enterprise->bank_account_usd,
                                'bank_account_owner_vnd' => $enterprise->bank_account_owner_vnd,
                                'bank_account_owner_usd' => $enterprise->bank_account_owner_usd,
                                'registration_number'    => $enterprise->registration_number,
                                'registration_date'      => $enterprise->registration_date,
                                'registration_city'      => $enterprise->registration_city,
                                'deleted'                => $enterprise->deleted,
                                'professions_id'         => $enterprise->professions_id,
                                'currency'               => $enterprise->currency,
                                'application'            => $enterprise->application,
                            ));
        return $this->_conn->lastInsertId($this->_prefix . 'enterprise');
    }

    public function find($offset, $count, $exp = null)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('e' => $this->_prefix . 'enterprise'));
        if ($exp) {
            if (isset($exp['id'])) {
                $select->where('e.id = ?', $exp['id']);
            }
            if (isset($exp['deleted'])) {
                $select->where('e.deleted = ?', $exp['deleted']);
            }
        }
        $rs = $select->order('e.id DESC')
                    ->limit($count, $offset)
                    ->query()
                    ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function count($exp = null)
    {
        $select = $this->_conn
                        ->select()
                        ->from(array('e' => $this->_prefix . 'enterprise'), array('num_enterprise' => 'COUNT(*)'));
        if ($exp) {
            if (isset($exp['id'])) {
                $select->where('e.id = ?', $exp['id']);
            }
            if (isset($exp['deleted'])) {
                $select->where('e.deleted = ?', $exp['deleted']);
            }
        }
        return $select->query()->fetch()->num_enterprise;
    }

    public function getById($id){
        $row = $this->_conn
                    ->select()
                    ->from(array('e' => $this->_prefix . 'enterprise'))
                    ->where('e.id = ?', $id)
                    ->where('e.deleted != ?', 1)
                    ->limit(1)
                    ->query()
                    ->fetch();
        return (null == $row) ? null : new Enterprise_Models_Enterprise($row);
    }

    public function update($enterprise)
    {
        return $this->_conn->update($this->_prefix . 'enterprise',
                                array(
                                    'frontend_active'        => $enterprise->frontend_active,
                                    'backend_active'         => $enterprise->backend_active,
                                    'company_name_vn'        => $enterprise->company_name_vn,
                                    'company_name_en'        => $enterprise->company_name_en,
                                    'contact_email'          => $enterprise->contact_email,
                                    'contact_tel'            => $enterprise->contact_tel,
                                    'contact_fax'            => $enterprise->contact_fax,
                                    'contact_address'        => $enterprise->contact_address,
                                    'bank_name_vnd'          => $enterprise->bank_name_vnd,
                                    'bank_name_usd'          => $enterprise->bank_name_usd,
                                    'bank_account_vnd'       => $enterprise->bank_account_vnd,
                                    'bank_account_usd'       => $enterprise->bank_account_usd,
                                    'bank_account_owner_vnd' => $enterprise->bank_account_owner_vnd,
                                    'bank_account_owner_usd' => $enterprise->bank_account_owner_usd,
                                    'registration_number'    => $enterprise->registration_number,
                                    'registration_date'      => $enterprise->registration_date,
                                    'registration_city'      => $enterprise->registration_city,
                                    'deleted'                => $enterprise->deleted,
                                    'professions_id'         => $enterprise->professions_id,
                                    'currency'               => $enterprise->currency,
                                    'application'            => $enterprise->application,
                                ),
                                array(
                                    'id = ?' => $enterprise->id,
                                ));
    }

    public function delete($enterprise){
        return $this->_conn->update($this->_prefix . 'enterprise',
                                array(
                                    'deleted'               => 1,
                                ),
                                array(
                                    'id = ?' => $enterprise->id,
                                ));
    }

    public function getProfessions(){
        $select = $this->_conn
                        ->select()
                        ->from(array('e' => $this->_prefix . 'enterprise_professions'))
                        ;
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function updateProfessions($professions){
        $sql = "Update {$this->_prefix}enterprise_professions set enterprise_id = " .
                "concat(enterprise_id,'{$professions['enterprise_id']},') where professions_id = {$professions['professions_id']}";
        return $this->_conn->query($sql);
    }

    public function updateProfessions1($professions){
        $sql = "Update {$this->_prefix}enterprise_professions set enterprise_id = " .
                "REPLACE(enterprise_id,',{$professions['enterprise_id']},',',') where professions_id = {$professions['professions_id']}";
        return $this->_conn->query($sql);
    }
}
